import java.util.Random;

//************************************************************
//	COPYRIGHT 2011 Bread Zeppelin - ALL RIGHTS RESERVED
//
// This file is the product of Bread Zeppelin and cannot be 
// reproduced, copied, or used in any shape or form without 
// the express written consent of Bread Zeppelin.
//************************************************************
//
//	$Id: Concurrent_Assg6
//
//	$Date: Feb 16, 2012
//
//	Reason: Preparation task class, all the behavior of the task is defined
//			below.
//
//*************************************************************

/** $Author: Ben Hare, Zach Huenink, Philipp Wittmaack, Peter Maresca */
/** $Revision: 1*/

// Default Package

// system imports

// project includes

//==============================================================

/**
 *
 *
 */
public class Preparation_Task implements Runnable {

	final Order custOrder;
	Restaurant restaurant = Restaurant.getInstance();

	/**
	 * Creates a prep task
	 * 
	 * @param order the order given to the waitress by the customer
	 */
	Preparation_Task(Order order){
		this.custOrder = order;
	}

	@Override
	public void run() {
		
		// Initiate CookTime Metric
		RestaurantMetrics.getInstance().beginCookOrderTime(this, System.currentTimeMillis());
		
		//sleeps for a random time of 20-30 min to cook food
		int time = new Random(System.currentTimeMillis()).nextInt(10);
		time = time * 60000;

		try {
			if(time > 1700000 ){
				Thread.currentThread().interrupt();
			}
			Thread.sleep( (1200000 + time) /Simulator.simulationSpeed);
			custOrder.prepare(true);
			System.out.println("	" + custOrder.getParty().getName() 
					+"'s Order has been cooked!");

			//Should move to hooks
			DeliverFood_Task deliverFood = new DeliverFood_Task(custOrder);
			restaurant.getFoodRunnerExecutor().execute(deliverFood);
			
			// Finalize CookTime Metric
			RestaurantMetrics.getInstance().finishCookOrderTime(this, System.currentTimeMillis());

		} catch (InterruptedException e) {
			custOrder.getParty().rageQuit();
			System.out.println(custOrder.getParty().getName() + " has left after" +
			"waiting for their food for too long");

			Table t = custOrder.getParty().getCustomerTable();
			restaurant.returnATable(t);
		}



	}

}
